//
//  RAPStatusCell.swift
//  COPYCAT-Weibo
//
//  Created by 闫萌 on 1/1/16.
//  Copyright © 2016 RendezvousAuParadis. All rights reserved.
//

import UIKit

class RAPStatusCell: UITableViewCell {
    
    var toolBarTopConstraint: Constraint? // NSLayoutConstraint
    
    var statusCellViewModel: RAPStatusCellViewModel? {
        didSet {
            originalView.statusCellViewModel = self.statusCellViewModel
            
            // 卸载约束
            toolBarTopConstraint?.uninstall()
            
            if statusCellViewModel?.statusModel.retweeted_status == nil {
                retweetView.hidden = true
                // 如果转发微博被隐藏
                toolBar.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolBarTopConstraint = make.top.equalTo(self.originalView.snp_bottom).constraint
                })
            } else {
                retweetView.hidden = false
                // 如果显示转发微博
                toolBar.snp_updateConstraints(closure: { (make) -> Void in
                    self.toolBarTopConstraint = make.top.equalTo(self.retweetView.snp_bottom).constraint
                })
                retweetView.statusCellViewModel = self.statusCellViewModel
            }
            toolBar.statusCellViewModel = self.statusCellViewModel
        }
    }
    
//    "<COPYCAT_Weibo.LayoutConstraint:0x7fb6828a0a20@/Users/Teddy_Yann/Desktop/COPYCAT-Weibo/COPYCAT-Weibo/COPYCAT-Weibo/Classes/View/Home/View/RAPStatusCellRetweetView.swift#23 UILabel:0x7fb68289f810.top == COPYCAT_Weibo.RAPStatusCellRetweetView:0x7fb68289ee20.top + 5.0>",
//    "<COPYCAT_Weibo.LayoutConstraint:0x7fb6828a0f50@/Users/Teddy_Yann/Desktop/COPYCAT-Weibo/COPYCAT-Weibo/COPYCAT-Weibo/Classes/View/Home/View/RAPStatusCellRetweetView.swift#23 UILabel:0x7fb68289f810.bottom <= COPYCAT_Weibo.RAPStatusCellRetweetView:0x7fb68289ee20.bottom>",
//    "<COPYCAT_Weibo.LayoutConstraint:0x7fb68295c040@/Users/Teddy_Yann/Desktop/COPYCAT-Weibo/COPYCAT-Weibo/COPYCAT-Weibo/Classes/View/Home/View/RAPStatusCell.swift#83 COPYCAT_Weibo.RAPStatusCellRetweetView:0x7fb68289ee20.top == COPYCAT_Weibo.RAPStatusCellOriginalView:0x7fb68293b2c0.bottom>",
//    "<COPYCAT_Weibo.LayoutConstraint:0x7fb68295d760@/Users/Teddy_Yann/Desktop/COPYCAT-Weibo/COPYCAT-Weibo/COPYCAT-Weibo/Classes/View/Home/View/RAPStatusCell.swift#92 COPYCAT_Weibo.RAPStatusCellToolBar:0x7fb6828a16e0.top == COPYCAT_Weibo.RAPStatusCellRetweetView:0x7fb68289ee20.bottom>",
//    "<COPYCAT_Weibo.LayoutConstraint:0x7fb682894620@/Users/Teddy_Yann/Desktop/COPYCAT-Weibo/COPYCAT-Weibo/COPYCAT-Weibo/Classes/View/Home/View/RAPStatusCell.swift#22 COPYCAT_Weibo.RAPStatusCellToolBar:0x7fb6828a16e0.top == COPYCAT_Weibo.RAPStatusCellOriginalView:0x7fb68293b2c0.bottom>"
    
    
    // MARK: - 懒加载控件
    lazy var originalView: RAPStatusCellOriginalView = {
        let orginalView = RAPStatusCellOriginalView()
        
        return orginalView
    }()
    
    lazy var retweetView: RAPStatusCellRetweetView = {
        let retweetView = RAPStatusCellRetweetView()
        
        return retweetView
    }()
    
    lazy var toolBar: RAPStatusCellToolBar = {
        let toolBar = RAPStatusCellToolBar()
        
        return toolBar
    }()
    
    // MARK: - 系统方法
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier) // 重写指定初始化方法，调用父类的方法
        backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        
        // 设置选中状态
        selectionStyle = .None
        contentView.addSubview(originalView)
        contentView.addSubview(retweetView)
        contentView.addSubview(toolBar)
        
        // 先加入父视图，再添加约束
        installConstraints()
    }
    
    func installConstraints() {
        /*
        原创微博部分
        */
        originalView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView).offset(kStatusCellMargin) // 向下移动，空出头部的距离
            make.leading.equalTo(contentView)
            make.trailing.equalTo(contentView)
        }
        
        /*
        转发微博部分
        */
        retweetView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.trailing.equalTo(contentView)
        }
        
        
        /*
        工具栏部分
        */
        toolBar.snp_makeConstraints { (make) -> Void in
            self.toolBarTopConstraint = make.top.equalTo(retweetView.snp_bottom).constraint
            make.leading.bottom.equalTo(contentView)
            make.trailing.equalTo(contentView)
            make.height.equalTo(30)
        }
        
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self) // 注意这个约束，不要试图约束cell的高度，因为它是自动算高的。不能加.offset(5)
            make.leading.trailing.equalTo(self)
        }
        
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
}
